#include "stdafx.h"

void FileMachine::AnalyzeFilePath()
{
	// path analysis
	m_fileNamePlusExt = m_filePath;
	m_fileNameNoExt = m_filePath;
	m_fileExt = L"";
	m_fileFolder = L"";
	m_filePathPlusNameNoExt = m_filePath;

	size_t pos = m_filePath.find_last_of(L".");
	if (pos != wstring::npos)
	{
		m_fileExt = m_filePath.substr(pos+1, m_filePath.length());
		
		size_t postmp = m_fileExt.find_last_of(L"\\");
		if (postmp != wstring::npos)
		{
			m_fileExt = L"";
		}
		else
		{
			for(wstring::iterator i = m_fileExt.begin(); i != m_fileExt.end(); ++i)
			{
				*i = (wchar_t)tolower(*i);
			}

			m_filePathPlusNameNoExt = m_filePath.substr(0, pos);
		}
	}

	pos = m_filePath.find_last_of(L"\\");
	if (pos != wstring::npos)
	{
		m_fileNamePlusExt = m_filePath.substr(pos+1, m_filePath.length());
		if (m_fileExt == L"")
		{
			m_fileNameNoExt = m_filePath.substr(pos+1, m_filePath.length() - pos);
		}
		else
		{
			m_fileNameNoExt = m_filePath.substr(pos+1, m_filePath.length() - pos - m_fileExt.length() -2);
		}
		m_fileFolder = m_filePath.substr(0, pos);
	}

	// file attributes analysis
	WIN32_FIND_DATA FileData;
	HANDLE hFind;

	hFind =	FindFirstFile(m_filePath.c_str(), &FileData);

	if (hFind == INVALID_HANDLE_VALUE)
	{
		wcout << L"FileMachine: FindFirstFile failed for ";
		FilesRefactorUtils::PrintUTF16ToStream(m_filePath, true, wcout);
		return;
	}
	
	FILETIME ft = FileData.ftCreationTime;
	m_creationTime = CConvert::ToWString(&ft);

	ft = FileData.ftLastAccessTime;
	m_lastAccessTime = CConvert::ToWString(&ft);

	FindClose(hFind);

	m_fileSize = FilesRefactorUtils::GetFileSize(m_filePath);

	if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
	{
		m_isFile = false;
	}
	else
	{
		m_isFile = true;
	}

}

void FileMachine::Path(wstring& retVal) 
{ 
	retVal = m_filePath; 
}

void FileMachine::Folder(wstring& retVal) 
{ 
	retVal = m_fileFolder; 
}

void FileMachine::Extension(wstring& retVal) 
{ 
	retVal = m_fileExt; 
}

void FileMachine::NamePlusExt(wstring& retVal) 
{	
	retVal = m_fileNamePlusExt;	
}

void FileMachine::NameNoExt(wstring& retVal)	
{ 
	retVal = m_fileNameNoExt; 
}

void FileMachine::PathPlusNameNoExt(wstring& retVal) 
{ 
	retVal = m_filePathPlusNameNoExt; 
}

void FileMachine::CreationTime(wstring& retVal) 
{ 
	retVal = m_creationTime; 
}

void FileMachine::LastAccessTime(wstring& retVal) 
{ 
	retVal = m_lastAccessTime; 
}

int FileMachine::GetLineCount(const wstring& fileName)
{
	const int FM_NORMAL = 9;
	const int FM_ONE = 5;

	if (!PathFileExists(wstring(fileName + FrogElementExt).c_str()))
	{
		return -1;
	}

	SYSTEMTIME utilsobj = {0};
	UtilsObjCreate (&utilsobj);
	utilsobj.wHour = 0;
	utilsobj.wMilliseconds = 0;
	utilsobj.wMinute = 0;
	utilsobj.wSecond = 0;

	FileMachine read(NULL, L"c:\\lala.txt");
    ifstream in(wstring(fileName + FrogElementExt).c_str(), ios::binary);
	if (in.bad())
	{
		printf("BBABABAABABABBAABBABABABABABBABABABABABAB\n");
	}
    //in.read((char*)&read, sizeof(read));
	in.seekg(sizeof(read));
	SYSTEMTIME utilsobj2 = {0};
	in.read((char*)&utilsobj2, sizeof(utilsobj2));
	utilsobj2.wHour = 0;
	utilsobj2.wMilliseconds = 0;
	utilsobj2.wMinute = 0;
	utilsobj2.wSecond = 0;

	in.close();

    double duplic1;
    double duplic2;

	CConvert::CConvertToDuplicator (&utilsobj, &duplic1);
	CConvert::CConvertToDuplicator (&utilsobj2, &duplic2);

	double dup = duplic1-duplic2;

	if (dup<0)
	{
		return (int)dup;
	}

	return (FM_NORMAL+FM_ONE) - (int)dup;
}

INT64 FileMachine::FileSize() 
{ 
	return m_fileSize; 
}

bool FileMachine::IsFile() 
{ 
	return m_isFile; 
}

void FileMachine::AppendToFileNameWithPath(const wstring& prefix, const wstring& postfix, wstring& retVal)
{
	retVal = m_fileFolder + L"\\" + prefix + m_fileNameNoExt + postfix;
	if (m_fileExt != L"")
	{
		retVal += L"." + m_fileExt;
	}
}

void FileMachine::MimeType(wstring& retVal)
{
	if ((m_fileExt == L"jpg") || (m_fileExt == L"jpeg"))
	{
		retVal = L"image/jpeg";
	}
	else if (m_fileExt == L"bmp")
	{
		retVal = L"image/bmp";
	}
	else if (m_fileExt == L"gif")
	{
		retVal = L"image/gif";
	}
	else if ((m_fileExt == L"tif") || (m_fileExt == L"tiff"))
	{
		retVal = L"image/tiff";
	}
	else if (m_fileExt == L"png")
	{
		retVal = L"image/png";
	}
	else
	{
		retVal = L"Undefined";
	}
}